﻿using BaseUi.ViewModel;
using HandyControl.Controls;
using HandyControl.Tools.Extension;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MessageBox = HandyControl.Controls.MessageBox;
using Window = System.Windows.Window;

namespace BaseUi
{
    /// <summary>
    /// 用户信息
    /// </summary>
    public partial class UserEditView : Window
    {
        public UserEditView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns>true:成功，false:取消</returns>
        public static bool Show(UserBase userBase, CrudEnum crudEnum)
        {
            bool isOk = false;

            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                var view = new UserEditView();

                try
                {
                    if (crudEnum == CrudEnum.Read)
                        view.qr.IsEnabled = false;

                    var rs = AuthDb.db.Queryable<RoleBase>().Where(o => !o.IsAdmin).ToList();

                    view.textBox.Text = userBase.LoginName;
                    view.passwordBox.Password = userBase.Password;
                    view.checkComboBox.ItemsSource = rs;
                    view.checkComboBox.DisplayMemberPath = nameof(RoleBase.Name);
                    view.checkComboBox.SelectedValuePath = nameof(RoleBase.Id);

                    foreach (var item in userBase.RoleIds)
                    {
                        var aaa = rs.FirstOrDefault(o => o.Id == item);
                        if (aaa != null)
                            view.checkComboBox.SelectedItems.Add(aaa);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    view = null;
                    return;
                }

                view.qr.Click += (s, e) =>
                {
                    try
                    {
                        UserBase newuser = new UserBase()
                        {
                            Id = userBase.Id,
                            IsAdmin = userBase.IsAdmin,
                            LoginName = view.textBox.Text.Trim(),
                            Password = view.passwordBox.Password,
                            RoleIds = view.checkComboBox.SelectedItems.Cast<RoleBase>().Select(o => o.Id).ToList(),
                            Time = userBase.Time,
                        };

                        if (crudEnum == CrudEnum.Update)
                        {
                            if (string.IsNullOrEmpty(newuser.LoginName))
                            {
                                MessageBox.Show("请输入名称");
                                return;
                            }

                            if (AuthDb.db.Queryable<UserBase>().Where(o => o.Id != newuser.Id && o.LoginName == newuser.LoginName).Any())
                            {
                                MessageBox.Show("此存名称已存在");
                                return;
                            }

                            AuthDb.db.Updateable(newuser).ExecuteCommand();
                        }
                        else if (crudEnum == CrudEnum.Create)
                        {
                            newuser.Id = 0;
                            newuser.Time = DateTime.Now;

                            if (string.IsNullOrEmpty(newuser.LoginName))
                            {
                                MessageBox.Show("请输入名称");
                                return;
                            }
                            if (AuthDb.db.Queryable<UserBase>().Where(o => o.LoginName == newuser.LoginName).Any())
                            {
                                MessageBox.Show("此存名称已存在");
                                return;
                            }

                            AuthDb.db.Insertable(newuser).ExecuteCommand();
                        }
                        else
                        {
                            MessageBox.Show("不支持此操作");
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        return;
                    }

                    isOk = true;
                    view.Close();
                };

                view.ShowDialog();
                view = null;
            }));

            return isOk;
        }

        private void qx(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
